package com.example.administrator.newjiajiesong.main.activity.mvp.presenter;

import android.text.TextUtils;
import android.util.Log;


import com.example.administrator.newjiajiesong.BaseApplication;
import com.example.administrator.newjiajiesong.main.activity.mvp.contract.LoginContract;
import com.example.administrator.newjiajiesong.main.activity.mvp.model.LoginModelImpl;
import com.example.administrator.newjiajiesong.main.my.entity.TreeUserEntity;
import com.example.administrator.newjiajiesong.network.ApiCallBack;
import com.example.administrator.newjiajiesong.network.http.ApiUtils;
import com.example.administrator.newjiajiesong.utils.checkVersionsUtils.ProfileSpUtils;
import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.observers.DefaultObserver;

/**
 * Created by Administrator on 2017/04/19
 */

public class LoginPresenterImpl extends MvpBasePresenter<LoginContract.View> implements LoginContract.Presenter, LoginModelImpl.LoginIMSuccessListener {
    private LoginModelImpl mModel;

    public LoginPresenterImpl() {
        mModel = new LoginModelImpl();
        mModel.setLoginIMSuccessListener(this);
    }

    @Override
    public void initData() {
        if (getView() == null) {
            return;
        }
        String saveMobli = mModel.getSaveMobli();
        if (!TextUtils.isEmpty(saveMobli)) {
            getView().setMobile(saveMobli);
        }
        String savePassword = mModel.getSavePassword();
        if (!TextUtils.isEmpty(savePassword)) {
            getView().setPassWord(savePassword);
        }
    }

    @Override
    public void login() {
        if (getView() == null) {
            return;
        }
        //验证手机号码规范
        final String mobile = getView().getMobile();
        if (TextUtils.isEmpty(mobile) || mobile.length() != 11) {
            getView().toast("请输入正确的手机号码");
            return;
        }
        //验证密码规范
        final String passWord = getView().getPassWord();

        //验证验证规范
        String code = getView().getCode();
        if (TextUtils.isEmpty(code) || code.length() != 4) {
            getView().toast("请输入正确的验证码");
            return;
        }
        getView().showProgressDialog("登录中...");
        mModel.login(mobile, passWord, code, new ApiCallBack<TreeUserEntity>() {
            @Override
            protected void onSuccess(TreeUserEntity data, String message) {
                if (getView() == null) {
                    return;
                }
                //getView().dismissProgressDialog();
                mModel.setLogin(true);
                mModel.saveUserInfo(data);
                Log.d("LoginPresenterImpl: ", data.toString());
                //登陆IM
                mModel.logIM();
                getView().toast("登陆成功");
                //记住密码
                boolean savePassword = getView().isSavePassword();
                mModel.savePassword(savePassword ? mobile : null, savePassword ? passWord : null);

                //toLive(ProfileSpUtils.getInstance().getUserProfie().getDefault_live_id());
                //getView().startActivity(MainActivity.class);
            }

            @Override
            protected void onFailure(String error) {
                if (getView() == null) {
                    return;
                }
                getView().dismissProgressDialog();
                getView().toast(error);
            }
        });
    }


    @Override
    public void sendCode() {
        //验证手机号码规范
        if (getView() == null) {
            return;
        }
        String mobile = getView().getMobile();
        if (TextUtils.isEmpty(mobile) || mobile.length() != 11) {
            getView().toast("请输入正确的手机号码");
            return;
        }
        getView().showProgressDialog("验证码发送中");
        mModel.sendCode(LoginModelImpl.TYPE_LONG, mobile, new ApiCallBack() {
            @Override
            protected void onSuccess(Object responseData, String message) {
                if (getView() == null) {
                    return;
                }
                getView().dismissProgressDialog();
                getView().toast("发送成功,请注意查收");
                //发送成功开始倒计时
                final int count = 60;
                Observable.interval(0, 1, TimeUnit.SECONDS)//设置0秒延迟,每隔1秒发送一次数据
                        .take(count + 1)//循环61次
                        .map(new Function<Long, Long>() {
                            @Override
                            public Long apply(@NonNull Long aLong) throws Exception {
                                return count - aLong;
                            }
                        })
                        .observeOn(AndroidSchedulers.mainThread())
                        .doOnSubscribe(new Consumer<Disposable>() {
                            @Override
                            public void accept(@NonNull Disposable disposable) throws Exception {
                                if (getView() == null) {
                                    return;
                                }
                                //在发送数据时按钮不可点击
                                getView().setCodeBtnEnable(false);
                            }
                        })
                        .subscribe(new DefaultObserver<Long>() {
                            @Override
                            public void onNext(Long aLong) {
                                if (getView() == null) {
                                    return;
                                }
                                getView().setCodeBtnText("(" + aLong + "s)获取验证码");
                            }

                            @Override
                            public void onError(Throwable e) {
                                if (getView() == null) {
                                    return;
                                }
                                //异常,停止计时
                                getView().setCodeBtnEnable(true);
                                getView().setCodeBtnText("获取验证码");
                                getView().toast(e.getMessage());
                            }

                            @Override
                            public void onComplete() {
                                if (getView() == null) {
                                    return;
                                }
                                getView().setCodeBtnEnable(true);
                                getView().setCodeBtnText("获取验证码");

                            }
                        });
            }

            @Override
            protected void onFailure(String error) {
                if (getView() != null) {
                    getView().dismissProgressDialog();
                    getView().toast(error);
                }
            }
        });

    }

    @Override
    public void weChatLogin() {

    }

    @Override
    public void toLive(String c) {

    }


    @Override
    public void LoginIMSuccess(boolean isSuccess) {

    }
}